package com.jgc.threadlocal;

/**
 * Created by Administrator on 2022/3/20.
 */
public class ThreadLocalExample {
    /**
     * 由于它们访问的是一个ThreadLocal对象，因此这两个线程都无法看到对方保存的值。也就是说，它们存取的是两个不同的值
     * @param args
     */
    public static void main(String[] args) {
        MyRunnable myRunnable = new MyRunnable();
        Thread a = new Thread(myRunnable);
        Thread b = new Thread(myRunnable);
        a.start();
        b.start();
    }
    public static class MyRunnable implements  Runnable{
        private ThreadLocal threadLocal = new ThreadLocal();
        @Override
        public void run() {
            threadLocal.set((int)(Math.random()*100D));
//            threadLocal.remove();//可以释放
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(threadLocal.get());
        }
    }
}
